package com.dog.modules.message.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.utils.PageUtils;
import com.dog.modules.message.domain.entity.SmsLog;
import com.dog.modules.message.mapper.SmsLogMapper;
import com.dog.modules.message.domain.query.SmsLogQuery;
import com.dog.modules.message.domain.vo.SmsLogVO;
import com.dog.modules.message.service.SmsLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sms_log(短信日志)】的数据库操作Service实现
 * @createDate 2024-01-24 17:24:07
 */
@Slf4j
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {

    @Override
    public PageResult<SmsLogVO> page(SmsLogQuery query) {
        PageResult<SmsLogVO> pageResult = new PageResult<>();
        PageResult<SmsLog> smsLogPageResult = PageUtils.getPage(query, d -> this.list(getWrapper(query)));
        List<SmsLogVO> smsLogVOList = BeanUtil.copyToList(smsLogPageResult.getRows(),SmsLogVO.class);
        BeanUtil.copyProperties(smsLogPageResult,pageResult);
        pageResult.setRows(smsLogVOList);
        return pageResult;
    }

    private LambdaQueryWrapper<SmsLog> getWrapper(SmsLogQuery query){
        LambdaQueryWrapper<SmsLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getPlatform() != null, SmsLog::getPlatform, query.getPlatform());
        wrapper.like(query.getPlatformId() != null, SmsLog::getPlatformId, query.getPlatformId());
        wrapper.orderByDesc(SmsLog::getId);
        return wrapper;
    }
}




